// vi:set ft=javascript ff=dos ts=4 sts=4 sw=4 et:

// USAGE:
//   mask.png and image.jpg to {foobar2000 folder}\images

// ==PREPROCESSOR==
// @name "ApplyMask"
// @author "T.P Wang"
// ==/PREPROCESSOR==

var g_img_mask = gdi.Image(fb.FoobarPath + "images\\mask.png");
var g_img = gdi.Image(fb.FoobarPath + "images\\image.jpg");
var ww = 0,
    wh = 0;


function on_paint(gr) {
    if (g_img) {
        // Keep aspect ratio
        var scale_w = ww / g_img.Width;
        var scale_h = wh / g_img.Height;
        var scale = Math.min(scale_w, scale_h);
        var pos_x = 0,
            pos_y = 0;

        if (scale_w < scale_h) pos_y = (wh - g_img.height * scale) / 2;
        else if (scale_w > scale_h) pos_x = (ww - g_img.Width * scale) / 2;

        gr.DrawImage(g_img, pos_x, pos_y, g_img.Width * scale, g_img.Height * scale, 0, 0, g_img.Width, g_img.Height);
    }
}

function on_size() {
    ww = window.Width;
    wh = window.Height;
}

// Init
(function(){
    // mask *MUST* be the same size as g_img
    var mask = g_img_mask.Resize(g_img.Width, g_img.Height);

    // Apply mask image to g_img
    g_img.ApplyMask(mask);

    // Dispose() method is added in WSH Panel Mod 1.1.10, in order to free unused resource
    // So, now CollectGarbage() is no longer encouraged
    mask.Dispose();
})();

